#! /bin/bash


OLD_IFS=${IFS}
IFS=';'
read input
set -- $input
if [ $# -lt 9 ]; then
   echo "invalid number of input parameters"
   exit 1
fi
rcmd=$1
p_user=$2
p_passwd=$3
s_user=$4
s_passwd=$5
p_path=$6
s_path=$7
p_sid=$8
s_sid=$9

os=`uname -s`
otab=/etc/oratab

awkc=awk
if [ $os = "SunOS" ]; then
   otab=/var/opt/oracle/oratab
   awkc=nawk
fi

#if [ $sid = "dummydbwithhome" ]; then
#    oraclehome=$orahome
#else
oraclehome=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-home" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$oraclehome" ];then
    oraclehome=`grep -wi "$sid:"  $otab | grep "^$sid" | head -n1 | cut -f2 -d":"`
fi
#fi

TNSDIR=''
#if [ -z "$nlistener" ]; then
TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i listener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#else
#   TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i $nlistener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#   if [ ! -d "$TNSDIR" ]; then
#      TNSDIR=`ps -ef | grep tns | grep -v grep | grep -i -w $nlistener | $awkc NR==1 |  $awkc -F"/bin" '{print $(NF-1)}' | awk '{print $NF}'`
#   fi
#fi

TNS_ADMIN=$TNSDIR/network/admin
#tnsdir="/act/act_scripts/TNSADMIN_"$sid".tns"
#if [ -f $tnsdir ]; then
#   TNS_ADMIN=`cat $tnsdir`
#fi
orauser=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-user" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$orauser" ];then
    orauser=`ps -ef | grep ora_pmon | grep -v grep | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$orauser" ];then
    orauser=oracle
fi

PATH=$oraclehome/bin:$PATH
ORACLE_HOME=$oraclehome
ORACLE_SID=$s_sid
LD_LIBRARY_PATH=$oraclehome/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export PATH
export ORACLE_HOME
export ORACLE_SID
export ORACLE_HOME
export TNS_ADMIN
export LD_LIBRARY_PATH

#check orauser is valid
if [ ! -z "$orauser" ]; then
    result=`id $orauser`
    if [ $? -ne 0 ]; then
       uid=$orauser
       orauser=`$awkc -v val=$uid -F ":" '$3==val{print $1}' /etc/passwd`
       result=`id $orauser`
       if [ $? -ne 0 ]; then
          orauser=''
       fi
    fi
fi


rman=`echo "$oraclehome/bin/rman target $p_user/$p_passwd@hlprimary auxiliary $s_user/$s_passwd@hlstandby1"`

# 创建数据文件路径
if [ ! -e "$s_path" ];then
    mkdir -p $s_path/$s_sid
fi
chown -R $orauser $s_path

s_arch=/u01/arch
if [ ! -e "$s_arch" ];then
    mkdir -p $s_arch
fi
chown -R $orauser $s_arch

oraBase=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-base" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$oraBase" ];then
    oraBase=/u01/app
fi

adump=$oraBase/admin/$s_sid/adump
if [ ! -e "$adump" ];then
    mkdir -p $adump
fi
chown -R $orauser $oraBase/admin

#check rman cmd is a file
if [ $? = 0 ]; then
    rcmd=`cat $rcmd`
fi

rmanfile="/opt/howlink/lb-agent/scripts/oracle/dg/"$s_sid"_dumpdg.rcv"

if [ "$os" != "Linux" ]; then
   rcmd=`echo "$rcmd" | sed 's/;/;\\\n/g'`
fi
   rcmd=`echo "$rcmd" | sed  's#$p_sid#'${p_sid}'#g'`
   rcmd=`echo "$rcmd" | sed  's#$s_sid#'${s_sid}'#g'`
   rcmd=`echo "$rcmd" | sed  's#$p_path#'${p_path}'#g'`
   rcmd=`echo "$rcmd" | sed  's#$s_path#'${s_path}'#g'`

echo "$rcmd" > $rmanfile


chmod 644 "$rmanfile"

#check log file
rlog="/opt/howlink/lb-agent/log/${s_sid}_dg.log"
if [ ! -f "$rlog" ]; then
   touch "$rlog"
else
    echo "" > "$rlog"
fi

if [ "$os" = "SunOS" ]; then
    #get group for the user
    grp=`/usr/xpg4/bin/id -gn $orauser`
    if [ ! -z "$grp" ]; then
       chown $orauser:$grp $rlog
       chmod 666 $rlog
    fi
else
    grp=`id -gn $orauser`
    if [ -z "$grp" ]; then
       echo "failed to get group for $grp"
       return 1
    fi
    chown $orauser:$grp $rlog
    chmod 666 $rlog
fi

NLS_DATE_FORMAT="yyyymmddhh24mi"
NLS_LANG=AMERICAN_AMERICA.US7ASCII
export NLS_DATE_FORMAT
export NLS_LANG

if [ "$os" != "Linux" ]; then
    retval=`su $orauser -c "$rman @$rmanfile log=$rlog append"`
else
    retval=`su -m $orauser -c "$rman @$rmanfile log=$rlog append"`
fi

su -m $orauser -c "$oraclehome/bin/sqlplus -s / as sysdba << EOF
set heading off
set feedback off
shutdown immediate;
startup;
alter database recover managed standby database using current logfile disconnect;
exit;
EOF
"

status=$?

if [ $status -ne 0 ]; then
    echo "Error: $retval"
else
    echo $retval
fi

IFS="$OLD_IFS"
#rm $rmanfile
exit $status